<?php

/**
 * Description of address
 * 
 * @author 叶风
 * Qq:    355007778
 * Email: ye.fn@163.com
 */

namespace c\user;
\YF_f::initC('\c\auth\permission')->login();
\lang::load('user');

class address extends \control
{
    var $table = 'user_address';
    function index_home()
    {
        $res = $this->db->get_page(array(), $this->table, '*', 'user_id='. \YF_f::$login_user_id);
        $this->loadTemplate('user/address/home', array('res'=>$res));
        $this->display('user');
    }
    function index_post()
    {
        $this->loadTemplate('user/address/post');
        $this->display('');
    }
    function index_save()
    {
        $data = $_POST['d'];
        $aid = \YF_f::initC('\c\area\g')->toID($data['area']);
        if (!$aid)
            ajax_json(\lang::g ('invalid area'));
        
        $data['_area_id'] = $aid;
        $data['user_id'] = \YF_f::$login_user_id; 
        $data['zipcode'] || $data['zipcode'] = '000000';
        if ($this->db->save($this->table, $data))
		 {
			$id = $this->db->insert_id();
			\log::write($this->table, 'add', $id);
			ajax_json(array('status'=>TRUE, 'id'=>$id));
		 }
        ajax_json(\lang::g('save fail')); 
    }
    function index_del()
    {
        $id = intval($_GET['id']);
        $b = $this->db->del($this->table, "id IN($id) AND user_id=". \YF_f::$login_user_id);
		if ($b)
		 {
			\log::write($this->table, 'del', $id);
			ajax_json(TRUE);	
		 }
		ajax_json(\lang::g( 'Over'));
    }
    
    protected function get()
    {
        return $this->db->get($this->table, '*', 'user_id='. \YF_f::$login_user_id, 'modified_time DESC');
    }
    
    // 调用
    function index_se()
    {
        $r = $this->get();
        $this->loadTemplate('user/address/select', array('res'=>$r));
        $this->display('');
    }
}
